<%@ page import="com.party.core.model.system.TargetType" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="../../include/tag.jsp" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <title>${category == null ? '发布' : '编辑'}品牌</title>
    <%@include file="../../include/commonFile.jsp" %>
    <link rel="stylesheet" href="${ctx}/css/common/list.css">
    <link rel="stylesheet" href="${ctx}/css/ui/activity/publish_form.css">
    <style>
        .index-outside > section .section-main {
            min-width: 0px !important;
            padding: 15px;
        }

        .index-outside > section {
            padding-bottom: 0px !important;
        }

        .cover-content .cover-img {
            width: 100px;
            height: 100px;
        }
    </style>
</head>
<body>
<!--头部-->
<div class="index-outside">
    <!--内容-->
    <section class="no-fix">
        <div class="section-main">
            <!-- 正文请写在这里 -->
            <div class="add-form-content">
                <form id="myForm" class="layui-form mt20" method="post" action="${ctx}/mall/brand/save.do">
                    <input type="hidden" name="id" value="${mallGoodsBrand.id}"/>
                    <div class="layui-form-item">
                        <label class="layui-form-label">品牌名称<span class="f-verify-red">*</span></label>
                        <div class="layui-input-block" style="width: 200px">
                            <input type="text" name="name" lay-verify="name" autocomplete="off"
                                   placeholder="请输入品牌名称" class="layui-input" value="${mallGoodsBrand.name}"
                            >
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">品牌logo<span class="f-verify-red">*</span></label>
                        <div class="cover-content">
                            <input type="hidden" name="logo" id="logo" lay-verify="logo"
                                   value="${mallGoodsBrand.logo}"/>
                            <c:if test="${mallGoodsBrand == null || empty mallGoodsBrand.logo}">
                                <span id="cover-img" class="cover-img"
                                      style="background-image:url(${ctx}/image/posterImg.png)"></span>
                            </c:if>
                            <c:if test="${mallGoodsBrand != null && not empty mallGoodsBrand.logo}">
                                <span id="cover-img" class="cover-img"
                                      style="background-image:url('${mallGoodsBrand.logo}')"></span>
                            </c:if>
                            <a class="layui-btn layui-btn-danger" id="sel_pic_img">+选择图片</a>
                            <div class="form-word-aux">建议尺寸：800x450</div>
                        </div>
                    </div>
                </form>
            </div>
        </div><!--底部-->
    </section>
</div>

<script type="text/javascript" src="${ctx}/script/common/form_cache.js"></script>
<script>
    function ajaxSubmit(data, url, cb) {
        $.ajax({
            type: 'POST',
            async: false, // 使用同步的方法
            data: data,
            dataType: 'json',
            success: function (result) {
                typeof cb === 'function' && cb(result);
            },
            url: url
        });
    }

    function verify() {
        var flag = true;
        var name = $("[name=name]").val();
        if (!util.isValid(name)) {
            util.layerMsgError("品牌名称不能为空")
            return false;
        } else {
            function cb(res) {
                flag = res.success;
                if (!res.success) {
                    util.layerMsgError(res.description)
                    return false;
                }
            }

            ajaxSubmit({
                name: name,
                id: '${mallGoodsBrand.id}'
            }, '${ctx}/mall/brand/verifyName.do', cb);
        }
        var logo = $("[name=logo]").val();
        if (!util.isValid(logo)) {
            util.layerMsgError("品牌logo不能为空")
            return false;
        }
        return flag;
    }

    function doSubmit() {
        var flag = false;
        if (verify()) {
            ajaxSubmit($('#myForm').serialize(), $("#myForm").attr('action'), function (res) {
                flag = res.success;
                if (res.success) {
                    util.layerMsgSuccess("提交成功")
                } else {
                    util.layerMsgError(res.description)
                }
            });
        }
        return flag;
    }

    function openDialog() {
        $(parent.$.find(".layui-layer-iframe")[0]).css("width", "900px");
        $(parent.$.find(".layui-layer-iframe")[0]).css("height", "730px");
        $(parent.$.find(".layui-layer-iframe")[0]).find("iframe").css("height", "630px");
    }

    function closeDialog() {
        $(parent.$.find(".layui-layer-iframe")[0]).css("width", "800px");
        $(parent.$.find(".layui-layer-iframe")[0]).css("height", "570px");
        $(parent.$.find(".layui-layer-iframe")[0]).find("iframe").css("height", "470px");
    }

    $(function () {
        layui.use([ 'form', 'laydate' ], function() {

        });
        $('#sel_pic_img').click(function () {
            typeof openDialog === 'function' && openDialog();
            txz.openSelImg({
                min: 1,
                max: 1,
                info: '建议尺寸：800x450',
                type: '<%=TargetType.MALL.getCode()%>',
                cb: function (imgs) {
                    $('#cover-img').css('background-image', 'url(' + imgs[0].path + ')');
                    $('#logo').val(imgs[0].path);
                    typeof closeDialog === 'function' && closeDialog();
                },
                closeCb: function () {
                    typeof closeDialog === 'function' && closeDialog();
                }
            })
        })
    });
</script>
</body>
</html>